<template>
  <view>
    <view @click="getDraft(draft.tuanId)" v-for="(draft, index) in drafts" :key="index" class="draft-item">
      <text class="draft-title">{{ draft.tuanTitle.slice(0, 16) }}...</text>
      <view class="draft-content">{{ draft.tuanDetail.slice(0, 18) }}...</view>
      <image class="get-chat" src="/static/my-icons/chat.png" @click.stop="getChat(draft.tuanId)"/>
      <image class="get-receive-button" src="/static/my-icons/receive.png" @click.stop="getReceive(draft.tuanId, draft.question, draft.needAgree)"></image>
    </view>
    <view v-if="drafts.length === 0" class="no-drafts-message">
      空空如也~
    </view>
  </view>
</template>

<script>
export default {
  props: {
    farmerId: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      drafts: []
    };
  },
  onLoad() {
  
  },
  onShow() { // 添加 onShow 生命周期钩子
    //获取组团信息列表
    this.fetchDrafts();
  },
  methods: {
    //获取组团信息列表
    fetchDrafts() {
      const userId = uni.getStorageSync('userId')
      console.log("--------userId:", userId);
      uni.$http
        .get(`/tuan/getSend?userId=${userId}`)
        .then((res) => {
          console.log("查看组团", res.data);
          // 解析服务器返回的 JSON 数据
          const responseData = res.data;
          if (responseData.code === 200) {
            this.drafts = responseData.data;
          }
        })
        .catch((err) => {
          console.error('获取组团失败:', err);
        });
    },
    // 获取组团信息
    getDraft(tuanId) {
      console.log("Navigating to draft_info with tuanId:", tuanId);
      uni.navigateTo({
        url: `/subpkg/send_info/send_info?tuanId=${tuanId}`
      });
    },
    //获取聊天信息
    getChat(tuanId){
      uni.navigateTo({
          url: `/subpkg/chat_interface/chat_interface?tuanId=${tuanId}`
        });
    },
    // 获取收到的加入申请
    getReceive(tuanId, question, needAgree) {
      console.log("点击了收到的加入申请:", tuanId);
      if(needAgree === false){
        uni.showToast({
          title: '此组团没有设置加入验证',
          icon: 'none'
        })
      }else{
          uni.navigateTo({
          url: `/subpkg/receive_apply/receive_apply?tuanId=${tuanId}&question=${question}`
        });
      }
      
    }

  }
}
</script>

<style>
.draft-item {
  border-bottom: 1px solid #ccc;
  padding: 10px;
  position: relative;
}

.draft-title {
  font-size: 16px;
  font-weight: bold;
  display: block; 
}

.draft-content {
  font-size: 14px;
  color: #666;
  display: block; 
}


.no-drafts-message {
  text-align: center;
  margin-top: 20vh;
  color: #999;
}

.get-receive-button {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  background-color: transparent;
  border: none;
  width: 25px; /* 添加: 设置图片宽度 */
  height: 25px; /* 添加: 设置图片高度 */
}
.get-chat {
  position: absolute;
  right: 55px;
  top: 50%;
  transform: translateY(-50%);
  background-color: transparent;
  border: none;
  width: 25px; /* 添加: 设置图片宽度 */
  height: 25px; /* 添加: 设置图片高度 */
}
</style>